package cn.senjoeson.healthsport.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.senjoeson.healthsport.GreenDaoManager;
import cn.senjoeson.healthsport.R;
import cn.senjoeson.healthsport.bean.User;
import cn.senjoeson.healthsport.dao.UserDao;
import cn.senjoeson.healthsport.utils.SPUtils;

public class LoginActivity extends AppCompatActivity {

    private static final String TAG = "LoginActivity";
    @BindView(R.id.et_id)
    EditText mEtId;
    @BindView(R.id.et_pwd)
    EditText mEtPwd;
    @BindView(R.id.btn_login)
    Button mBtnLogin;
    @BindView(R.id.tv_forgetpwd)
    TextView mTvForgetpwd;
    @BindView(R.id.tv_register)
    TextView mTvRegister;
    public UserDao mUserDao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        if (checkLogin()) {
            startActivity(new Intent(LoginActivity.this, MainActivity.class));
            finish();
        } else {
            initView();
        }


    }

    private boolean checkLogin() {
        return SPUtils.getBoolean("isLogin", false);
    }

    private void initView() {
        mUserDao = GreenDaoManager.getInstance().getNewSession().getUserDao();


        mBtnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username = mEtId.getText().toString();
                String password = mEtPwd.getText().toString();
                if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) {
                    Toast.makeText(LoginActivity.this, "用户名或者密码不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                List<User> users = mUserDao.queryBuilder().where(UserDao.Properties.Username.eq(username)).list();
                if (users != null && users.size() > 0) {
                    User user1 = users.get(0);
                    if (!TextUtils.equals(users.get(0).getPassword(), password)) {
                        Toast.makeText(LoginActivity.this, "密码输入错误", Toast.LENGTH_SHORT).show();
                        return;
                    }

                    long userid = user1.getUserid();
                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                    intent.putExtra("userid", userid);
                    startActivity(intent);
                    SPUtils.saveBoolean("isLogin", true);
                    SPUtils.saveString("userid", userid + "");
                    finish();
                } else {
                    Toast.makeText(LoginActivity.this, "该账号尚未注册", Toast.LENGTH_SHORT).show();
                    return;
                }


            }
        });

        mTvRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
            }
        });
    }
}
